#include <QDebug>
#include <QLineEdit>
#include "actioneditdialog.h"
#include "ui_actioneditdialog.h"
#include "buttonrelay.h"
#include "relaytimer.h"

ActionEditDialog::ActionEditDialog(Layout *layout, RelayTimer *timer, RelayComputer *computer, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ActionEditDialog)
{
    ui->setupUi(this);

    QVariant userData;

    for (int i=0; i<layout->mButtons.count(); i++) {
        QByteArray ba;
        ba.append(layout->mButtons[i].mCId);
        ba.append(layout->mButtons[i].mBId);
        ba.append(layout->mButtons[i].mRId);
        ba.append(layout->mButtons[i].mRType);
        userData = QVariant(ba);
        ui->comboBoxName->addItem(QString("%1 - %2 %3 %4")
                                  .arg(layout->mButtons[i].mName)
                                  .arg(layout->mButtons[i].mCId)
                                  .arg(layout->mButtons[i].mBId)
                                  .arg(layout->mButtons[i].mRId),
                                  userData);
    }
    ui->comboBoxName->setEditable(false);
    connect(ui->comboBoxName, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));

    for (int h=0; h<24; h++) {
        for (int m=0; m<2; m++) {
            QString strTime;
            strTime.sprintf("%02d:%02d", h, m ? 30 : 0);
            ui->comboBoxTimeStart->addItem(strTime);
            ui->comboBoxTimeEnd->addItem(strTime);
        }
    }
    ui->comboBoxTimeStart->addItem(DISP_SUNRISE_STR);
    ui->comboBoxTimeStart->addItem(DISP_SUNSET_STR);
    ui->comboBoxTimeStart->setCurrentIndex(-1);
    ui->comboBoxTimeEnd->addItem(DISP_SUNRISE_STR);
    ui->comboBoxTimeEnd->addItem(DISP_SUNSET_STR);
    ui->comboBoxTimeEnd->setCurrentIndex(-1);

    connect(ui->checkBoxDayOfWeek, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
    ui->checkBoxSun->setEnabled(false);
    ui->checkBoxMon->setEnabled(false);
    ui->checkBoxTue->setEnabled(false);
    ui->checkBoxWed->setEnabled(false);
    ui->checkBoxThu->setEnabled(false);
    ui->checkBoxFri->setEnabled(false);
    ui->checkBoxSat->setEnabled(false);


    mpTimer = timer;
    if (timer && timer->uuid.length()) {
        QString name;
        int type = RELAY_TYPE_RELAY;
        for (int i=0; i<layout->mButtons.count(); i++) {
            if ((layout->mButtons[i].mCId == timer->mCId)
                    && (layout->mButtons[i].mBId == timer->mBId)
                    && (layout->mButtons[i].mRId == timer->mRId)) {
                name = QString("%1 - %2 %3 %4")
                        .arg(layout->mButtons[i].mName)
                        .arg(layout->mButtons[i].mCId)
                        .arg(layout->mButtons[i].mBId)
                        .arg(layout->mButtons[i].mRId);
                type = layout->mButtons[i].mRType;
                break;
            }
        }
        ui->comboBoxName->setCurrentIndex(ui->comboBoxName->findText(name));
        ui->comboBoxName->setEnabled(false);

        changeActionCombo(type);

        int idx = ui->comboBoxTimeStart->findText(timer->mStartTime);
        if (-1 < idx) {
            ui->comboBoxTimeStart->setCurrentIndex(idx);
        } else {
            if (TIME_SUNRISE_STR == timer->mStartTime) {
                ui->comboBoxTimeStart->lineEdit()->insert(DISP_SUNRISE_STR);
            } else if (TIME_SUNSET_STR == timer->mStartTime) {
                ui->comboBoxTimeStart->lineEdit()->insert(DISP_SUNSET_STR);
            } else {
                ui->comboBoxTimeStart->lineEdit()->insert(timer->mStartTime);
            }
        }
        ui->comboBoxStart->setCurrentIndex(ui->comboBoxStart->findData(timer->mStartAction));

        idx = ui->comboBoxTimeEnd->findText(timer->mEndTime);
        if (-1 < idx) {
            ui->comboBoxTimeEnd->setCurrentIndex(idx);
        } else {
            if (TIME_SUNRISE_STR == timer->mEndTime) {
                ui->comboBoxTimeEnd->lineEdit()->insert(DISP_SUNRISE_STR);
            } else if (TIME_SUNSET_STR == timer->mEndTime) {
                ui->comboBoxTimeEnd->lineEdit()->insert(DISP_SUNSET_STR);
            } else {
                ui->comboBoxTimeEnd->lineEdit()->insert(timer->mEndTime);
            }
        }
        ui->comboBoxEnd->setCurrentIndex(ui->comboBoxEnd->findData(timer->mEndAction));

        if (timer->mDayOfWeek) {
            ui->checkBoxDayOfWeek->setChecked(true);

            ui->checkBoxSun->setEnabled(timer->mDayOfWeek & 0x01);
            ui->checkBoxMon->setEnabled(timer->mDayOfWeek & 0x02);
            ui->checkBoxTue->setEnabled(timer->mDayOfWeek & 0x04);
            ui->checkBoxWed->setEnabled(timer->mDayOfWeek & 0x08);
            ui->checkBoxThu->setEnabled(timer->mDayOfWeek & 0x10);
            ui->checkBoxFri->setEnabled(timer->mDayOfWeek & 0x20);
            ui->checkBoxSat->setEnabled(timer->mDayOfWeek & 0x40);
        }
    } else {
        ui->comboBoxName->setCurrentIndex(-1);
        ui->comboBoxStart->setCurrentIndex(-1);
        ui->comboBoxEnd->setCurrentIndex(-1);
    }


    mpComputer = computer;
    if (computer && computer->uuid.length()) {

    } else {

    }
}

ActionEditDialog::~ActionEditDialog()
{
    delete ui;
}

void ActionEditDialog::onButtonClicked()
{
    ui->checkBoxSun->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxMon->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxTue->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxWed->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxThu->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxFri->setEnabled(ui->checkBoxDayOfWeek->isChecked());
    ui->checkBoxSat->setEnabled(ui->checkBoxDayOfWeek->isChecked());
}

void ActionEditDialog::onCurrentIndexChanged(int index)
{
    QByteArray ba = ui->comboBoxName->itemData(index).toByteArray();
    changeActionCombo(ba.at(3));
}

void ActionEditDialog::accept()
{
    if (mpTimer) {
        if ((0 > ui->comboBoxName->currentIndex())
                || (0 > ui->comboBoxStart->currentIndex())
                || (0 > ui->comboBoxEnd->currentIndex())) {
            return;
        }

        QString startTime = ui->comboBoxTimeStart->currentText();
        QString endTime = ui->comboBoxTimeEnd->currentText();
        if (startTime == endTime) {
            return;
        }
        if (false == RelayTimer::parseTime(startTime, NULL, NULL)) {
            if (DISP_SUNRISE_STR == startTime) {
                startTime = TIME_SUNRISE_STR;
            } else if (DISP_SUNSET_STR == startTime) {
                startTime = TIME_SUNSET_STR;
            } else {
                return;
            }
        }
        if (false == RelayTimer::parseTime(endTime, NULL, NULL)) {
            if (DISP_SUNRISE_STR == endTime) {
                endTime = TIME_SUNRISE_STR;
            } else if (DISP_SUNSET_STR == endTime) {
                endTime = TIME_SUNSET_STR;
            } else {
                return;
            }
        }

        QByteArray ba = ui->comboBoxName->itemData(ui->comboBoxName->currentIndex()).toByteArray();

        mpTimer->mCId = ba.at(0);
        mpTimer->mBId = ba.at(1);
        mpTimer->mRId = ba.at(2);

        mpTimer->mStartTime = startTime;
        mpTimer->mStartAction = ui->comboBoxStart->itemData(ui->comboBoxStart->currentIndex()).toInt();
        mpTimer->mEndTime = endTime;
        mpTimer->mEndAction = ui->comboBoxEnd->itemData(ui->comboBoxEnd->currentIndex()).toInt();
        mpTimer->mDayOfWeek = 0;
        if (ui->checkBoxDayOfWeek->isChecked()) {
            mpTimer->mDayOfWeek |= ui->checkBoxSun->isChecked() ? 0x01 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxMon->isChecked() ? 0x02 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxTue->isChecked() ? 0x04 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxWed->isChecked() ? 0x08 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxThu->isChecked() ? 0x10 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxFri->isChecked() ? 0x20 : 0;
            mpTimer->mDayOfWeek |= ui->checkBoxSat->isChecked() ? 0x40 : 0;
        }
    }

    QDialog::accept();
}

void ActionEditDialog::changeActionCombo(int type)
{
    ui->comboBoxStart->clear();
    ui->comboBoxEnd->clear();

    QVariant userData;

    userData = QVariant(RELAY_STATUS_OFF);
    ui->comboBoxStart->addItem("OFF", userData);

    userData = QVariant(RELAY_STATUS_OFF);
    ui->comboBoxEnd->addItem("OFF", userData);

    if (RELAY_TYPE_RELAY == type) {
        userData = QVariant(RELAY_STATUS_ON);
        ui->comboBoxStart->addItem("ON", userData);

        userData = QVariant(RELAY_STATUS_ON);
        ui->comboBoxEnd->addItem("ON", userData);
    } else {
        userData = QVariant(RELAY_STATUS_ON);
        ui->comboBoxStart->addItem(tr("대기"),  userData);
        userData = QVariant(RELAY_STATUS_AIR);
        ui->comboBoxStart->addItem(tr("송풍"),  userData);
        userData = QVariant(RELAY_STATUS_COOLING);
        ui->comboBoxStart->addItem(tr("냉방"),  userData);
        userData = QVariant(RELAY_STATUS_HEATING);
        ui->comboBoxStart->addItem(tr("난방"),  userData);

        userData = QVariant(RELAY_STATUS_ON);
        ui->comboBoxEnd->addItem(tr("대기"),  userData);
        userData = QVariant(RELAY_STATUS_AIR);
        ui->comboBoxEnd->addItem(tr("송풍"),  userData);
        userData = QVariant(RELAY_STATUS_COOLING);
        ui->comboBoxEnd->addItem(tr("냉방"),  userData);
        userData = QVariant(RELAY_STATUS_HEATING);
        ui->comboBoxEnd->addItem(tr("난방"),  userData);
    }
}
